package industry;

import industry.IndustryBuff.Industry;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import actives.ActiveManager;
import actives.ActiveType;
import main.Plugin;

public class IndustryHandler implements Listener {
	private static final Industry ARCHAEOLOGY = Industry.ARCHAEOLOGY;
	private static final Industry MINING = Industry.MINING;
	private static final Industry WOODCUTTING = Industry.WOODCUTTING;

	public IndustryHandler(Plugin plugin) {
		Bukkit.getPluginManager().registerEvents(this, plugin);
		new Farming(plugin);
		new Mining(plugin);
		new Woodcutting(plugin);
	}
	
	@EventHandler
	private void industryBuff(BlockDamageEvent event) {
		Player player = event.getPlayer();
		Block block = event.getBlock();
		String item = player.getItemInHand().getType().name();
		IndustryBuffManager.reset(player);
		if (item.contains("_AXE")) {
			if (block.getType().equals(Material.LOG)) {
				IndustryBuffManager.add(player, block, WOODCUTTING);
				ActiveManager.activate(player, ActiveType.WOODCUTTING, null);
			}	
		}
		if (item.contains("_PICKAXE")) {
			if (Mining.isMinable(block)) {
				IndustryBuffManager.add(player, block, MINING);
				ActiveManager.activate(player, ActiveType.MINING, null);
			}
		}
		if (item.contains("_SPADE")) {
			if (Archaeology.isDiggable(block)) {
				IndustryBuffManager.add(player, block, ARCHAEOLOGY);
				ActiveManager.activate(player, ActiveType.ARCHAEOLOGY, null);
			}
		}
	}
	
	@EventHandler
	private void refreshIndustryBuff(PlayerAnimationEvent event) {
		Player player = event.getPlayer();
		Block block = player.getTargetBlock(null, 5);
		String item = player.getItemInHand().getType().name();
		if (item.contains("_AXE")) {
			IndustryBuffManager.refresh(player, block, WOODCUTTING);
		}
		if (item.contains("_PICKAXE")) {
			IndustryBuffManager.refresh(player, block, MINING);
		}
		if (item.contains("_SPADE")) {
			IndustryBuffManager.refresh(player, block, ARCHAEOLOGY);			
		}
	}
	
	@EventHandler
	private void removeIndustryBuff(BlockBreakEvent event) {
		Player player = event.getPlayer();
		IndustryBuffManager.reset(player);
	}
	
	@EventHandler
	private void industryActives(PlayerInteractEvent event) {
		if (event.getAction().name().contains("RIGHT_CLICK")) {
			Player player = event.getPlayer();
			String item = player.getItemInHand().getType().name();
			if (item.contains("_AXE")) {
				ActiveManager.prime(player, ActiveType.WOODCUTTING);
			}
			if (item.contains("_PICKAXE")) {
				ActiveManager.prime(player, ActiveType.MINING);
			}
			if (item.contains("_SPADE")) {
				ActiveManager.prime(player, ActiveType.ARCHAEOLOGY);
			}
		}
	}
}
